<template>
  <el-form
    ref="formRef"
    :model="formData"
    :rules="formRules"
    v-loading="formLoading"
    label-width="0px"
    :inline-message="true"
  >
    <el-table :data="formData" class="-mt-10px">
      <el-table-column label="序号" type="index" width="100" />
       <el-table-column label="任务单明细编号" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.assignmentDetailId`" :rules="formRules.assignmentDetailId" class="mb-0px!">
            <el-input v-model="row.assignmentDetailId" placeholder="请输入任务单明细编号" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="房产编号" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.houseId`" :rules="formRules.houseId" class="mb-0px!">
            <el-input v-model="row.houseId" placeholder="请输入房产编号" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="巡检父项编号" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.patrolPid`" :rules="formRules.patrolPid" class="mb-0px!">
            <el-input v-model="row.patrolPid" placeholder="请输入巡检父项编号" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="巡检项编号" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.patrolId`" :rules="formRules.patrolId" class="mb-0px!">
            <el-input v-model="row.patrolId" placeholder="请输入巡检项编号" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="巡检结果" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.patrolResult`" :rules="formRules.patrolResult" class="mb-0px!">
            <el-input v-model="row.patrolResult" placeholder="请输入巡检结果" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="巡检人" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.inspector`" :rules="formRules.inspector" class="mb-0px!">
            <el-input v-model="row.inspector" placeholder="请输入巡检人" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="巡检日期" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.inspectDate`" :rules="formRules.inspectDate" class="mb-0px!">
            <el-date-picker
              v-model="row.inspectDate"
              type="date"
              value-format="x"
              placeholder="选择巡检日期"
            />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="异常定位一" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.location1`" :rules="formRules.location1" class="mb-0px!">
            <el-input v-model="row.location1" placeholder="请输入异常定位一" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="异常定位二" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.location2`" :rules="formRules.location2" class="mb-0px!">
            <el-input v-model="row.location2" placeholder="请输入异常定位二" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="异常描述" min-width="400">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.description`" :rules="formRules.description" class="mb-0px!">
            <Editor v-model="row.description" height="150px" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="异常描述补充" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.descriptionExtra`" :rules="formRules.descriptionExtra" class="mb-0px!">
            <el-input v-model="row.descriptionExtra" placeholder="请输入异常描述补充" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="异常图片" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.fileUrls`" :rules="formRules.fileUrls" class="mb-0px!">
            <el-input v-model="row.fileUrls" placeholder="请输入异常图片" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="处理方法" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.processMethod`" :rules="formRules.processMethod" class="mb-0px!">
            <el-input v-model="row.processMethod" placeholder="请输入处理方法" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="是否核查更新" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.checked`" :rules="formRules.checked" class="mb-0px!">
            <el-radio-group v-model="row.checked">
                <el-radio value="1">请选择字典生成</el-radio>
            </el-radio-group>
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column align="center" fixed="right" label="操作" width="60">
        <template #default="{ $index }">
          <el-button @click="handleDelete($index)" link>—</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-form>
  <el-row justify="center" class="mt-3">
    <el-button @click="handleAdd" round>+ 添加PI 核查管理明细</el-button>
  </el-row>
</template>
<script setup lang="ts">
import { CheckApi } from '@/api/pi/check'

const props = defineProps<{
  checkId: number // 核查单编号（主表的关联字段）
}>()
const formLoading = ref(false) // 表单的加载中
const formData = ref<any[]>([])
const formRules = reactive({
  checkId: [{ required: true, message: '核查单编号不能为空', trigger: 'blur' }],
  checked: [{ required: true, message: '是否核查更新不能为空', trigger: 'blur' }],
})
const formRef = ref() // 表单 Ref

/** 监听主表的关联字段的变化，加载对应的子表数据 */
watch(
  () => props.checkId,
  async (val) => {
    // 1. 重置表单
    formData.value = []
    // 2. val 非空，则加载数据
    if (!val) {
      return;
    }
    try {
      formLoading.value = true
      formData.value = await CheckApi.getCheckDetailListByCheckId(val)
    } finally {
      formLoading.value = false
    }
  },
  { immediate: true }
)

/** 新增按钮操作 */
const handleAdd = () => {
  const row = {
    id: undefined,
    checkId: undefined,
    assignmentDetailId: undefined,
    houseId: undefined,
    patrolPid: undefined,
    patrolId: undefined,
    patrolResult: undefined,
    inspector: undefined,
    inspectDate: undefined,
    location1: undefined,
    location2: undefined,
    description: undefined,
    descriptionExtra: undefined,
    fileUrls: undefined,
    processMethod: undefined,
    checked: undefined,
  }
  row.checkId = props.checkId as any
  formData.value.push(row)
}

/** 删除按钮操作 */
const handleDelete = (index) => {
  formData.value.splice(index, 1)
}

/** 表单校验 */
const validate = () => {
  return formRef.value.validate()
}

/** 表单值 */
const getData = () => {
  return formData.value
}

defineExpose({ validate, getData })
</script>